/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.user.web;

import com.nc.kyiv.yellow.sociallibrary.beans.user.UserBeanLocal;
import com.nc.kyiv.yellow.sociallibrary.beans.user.UserBeanLocalHome;
import com.nc.kyiv.yellow.sociallibrary.dao.LibUser;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author My
 */
public class UserByLoginServlet extends HttpServlet {
    
    private UserBeanLocal userBean = null;
    String name;
    String pass;
    LibUser user;
    
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        userBean = lookupUserBeanLocal();
        Map params = new HashMap<String, String>();
        params.put("login", request.getParameter("login"));
        params.put("pass", request.getParameter("pass"));
        
        Map result = userBean.getUserByLoginAndPass(params);

        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UserByLoginServlet</title>");
            out.println("</head>");
            out.println("<body>");
//            if ((user = userBean.getUserByLogin(request.getParameter("login"))) != null) {
//                name = user.getFirstName();
//                pass = user.getPass();
//                if (request.getParameter("pass").equals(pass)) {
//                    out.println("Hello, " + name);
//                } else {
//                    out.println("Bad login or password");
//                }
//            } else {
//                out.println("Bad login or password(No user found)");
//            }
            if (result != null){
                out.println(result.get("first_name"));
            } else{
                out.println("Bad login or password(No user found)");
            }
            
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
        private UserBeanLocal lookupUserBeanLocal() {
        try {
            Context c = new InitialContext();
            UserBeanLocalHome ub = (UserBeanLocalHome) c.lookup("java:comp/env/UserBean");
            return ub.create();
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        } catch (CreateException ce) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ce);
            throw new RuntimeException(ce);
        }
    }
}
